package com.djx.基础;

import org.apache.zookeeper.*;
import org.apache.zookeeper.data.Stat;
import org.junit.Test;

import java.io.IOException;
import java.util.List;
import java.util.concurrent.CountDownLatch;

/**
 * zookepper常用API
 * 教程:
 * https://www.bilibili.com/video/BV1M741137qY?p=13
 */

//addauth digest super:admin
public class MyZookepper01 {

    //自己电脑的
    private String myIP = "192.168.15.132:2181";
    //公司电脑的
    private String compIP = "192.168.158.128:2181";
    private String whichPerformed = "192.168.15.132:2181,192.168.15.132:2182,192.168.15.132:2183";

    public ZooKeeper zooKeeper;

    /**
     * zookeeper创建过程是异步,当执行完这段代码并不代表与客户端之间的连接创建成功了
     */ {
        try {
            zooKeeper = new ZooKeeper(myIP, 50000, new Watcher() {
                public void process(WatchedEvent watchedEvent) {
                    if (watchedEvent.getState() == Event.KeeperState.SyncConnected) {
                    }
                }
            });
            System.out.println("zookeeper连接成功!!!");
            System.out.println("连接sessionID: " + zooKeeper.getSessionId());

        } catch (IOException e) {
            e.printStackTrace();
            System.out.println(e.getMessage());
        }

    }

    /**
     * 同步方式
     *
     * @throws Exception
     */
    @Test
    public void syncCreate() throws Exception {
        System.out.println(zooKeeper.create("/aaa", "aaaValue".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT));
        zooKeeper.close();
        System.out.println("zookeeper连接关闭");
    }

    /**
     * 异步方式
     *
     * @throws Exception
     */
    @Test
    public void asyncCreate() throws Exception {
        System.out.println(zooKeeper.create("/aaa", "aaaValue".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT));
        zooKeeper.close();
        System.out.println("zookeeper连接关闭");
    }


    @Test
    public void createChildren() throws Exception {
        System.out.println(zooKeeper.create("/aaa/aChild", "aChildValue".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT));
        zooKeeper.close();
        System.out.println("zookeeper连接关闭");
    }

    @Test
    public void getParentNode() throws Exception {
        System.out.println(new String(zooKeeper.getData("/aaa", false, null)));
        zooKeeper.close();
        System.out.println("zookeeper连接关闭");
    }

    @Test
    public void getChildrenNode() throws Exception {
        List<String> children = zooKeeper.getChildren("/aaa", false, null);
        for (String child : children) {
            System.out.println("子节点的值为" + child);
        }
        zooKeeper.close();
        System.out.println("zookeeper连接关闭");
    }

    @Test
    public void setNode() throws Exception {
        Stat stat = zooKeeper.setData("/aaa", "aValue2".getBytes(), -1);
        System.out.println(stat);
        zooKeeper.close();
        System.out.println("zookeeper连接关闭");
    }

    @Test
    public void setChildNode() throws Exception {
        Stat stat = zooKeeper.setData("/aaa/aChild", "aValue2".getBytes(), -1);
        System.out.println(stat);
        zooKeeper.close();
        System.out.println("zookeeper连接关闭");
    }

    @Test
    public void exists() throws Exception {
        int version = zooKeeper.exists("/aaa", false).getVersion();
        System.out.println(version);
        System.out.println(zooKeeper.exists("/aaa", false));
        zooKeeper.close();
        System.out.println("zookeeper连接关闭");
    }

    @Test
    public void deleteNode() throws Exception {
        System.out.println(zooKeeper.create("/djx", "djxvalue".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT));

        System.out.println(new String(zooKeeper.getData("/aaa", false, null)));

        zooKeeper.delete("/djx", -1);
        System.out.println("删除节点成功!");
        //删除成功会报空指针
        System.out.println("节点是否存在:" + zooKeeper.exists("/djx", false).getVersion());
        zooKeeper.close();
        System.out.println("zookeeper连接关闭");
    }


    @Test
    public void removeWatch() throws Exception {
        System.out.println("删除监视器");
        zooKeeper.close();
        System.out.println("zookeeper连接关闭");
    }

    @Test
    public void removeAllWatch() throws Exception {
        System.out.println("删除所有监视器");
        zooKeeper.close();
        System.out.println("zookeeper连接关闭");
    }

    //stat注册监听器
    @Test
    public void statWatch() throws Exception {

        zooKeeper.close();
        System.out.println("zookeeper连接关闭");
    }

    /**
     * stat -w path 监听
     * ls -w path 监听子节点的删除和创建
     * (ls2)ls -s path 监听子节点的创建和删除
     */

}

